package org.example.managementsystem.repository;


import org.example.managementsystem.model.ProviderComplaintReview;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 投诉审核记录数据访问对象
 * @author 王振祎
 * @date 2025-05-18
 */
@Repository
public class ProviderComplaintReviewDao {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public ProviderComplaintReviewDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 插入投诉审核记录
     * @param review 投诉审核记录
     * @return 插入的记录的 id
     */
    public int insertReview(ProviderComplaintReview review) {
        String sql = "INSERT INTO provider_complaint_review (complaint_id, is_approved, description, rating_deduction) " +
                "VALUES (?, ?, ?, ?)";

        jdbcTemplate.update(sql, review.getComplaintId(), review.isApproved(), review.getDescription(), review.getRatingDeduction());

        // 获取刚刚插入的 id
        // 这里由于id是刚刚插入表后再自增生成的，所以要修改Dao使得插入方法返回的是id
        String getLastIdSql = "SELECT LAST_INSERT_ID()";
        int lastInsertId = jdbcTemplate.queryForObject(getLastIdSql, Integer.class);
        return lastInsertId;
    }

    /**
     * 更新投诉审核记录
     * @param review 投诉审核记录
     */
    public void updateReview(ProviderComplaintReview review) {
        String sql = "UPDATE provider_complaint_review SET is_approved = ?, description = ?, rating_deduction = ? " +
                "WHERE complaint_id = ?";
        jdbcTemplate.update(sql, review.isApproved(), review.getDescription(), review.getRatingDeduction(), review.getComplaintId());
    }

    public void updateReview(int complaintId, boolean isApproved, String description, int ratingDeduction) {
        String sql = "UPDATE provider_complaint_review SET is_approved = ?, description = ?, rating_deduction = ? " +
                "WHERE complaint_id = ?";
        jdbcTemplate.update(sql, isApproved, description, ratingDeduction, complaintId);
    }

    /**
     * 获取所有投诉审核记录
     * @return
     */
    public List<ProviderComplaintReview> getAllReviews(){
        String sql = "SELECT * FROM provider_complaint_review";
        return jdbcTemplate.query(sql, new ProviderComplaintReviewRowMapper());
    }

    /**
     * 根据投诉ID获取投诉审核记录
     * @param complaintId 投诉ID
     * @return 投诉审核记录
     */
    public ProviderComplaintReview getReviewByComplaintId(int complaintId) {
        String sql = "SELECT * FROM provider_complaint_review WHERE complaint_id = ?";
        try {
            return jdbcTemplate.queryForObject(sql, new ProviderComplaintReviewRowMapper(), complaintId);
        } catch (EmptyResultDataAccessException e) {
            return null; // 返回空
        }
    }


    /**
     * 投诉审核记录映射器
     */
    private static class ProviderComplaintReviewRowMapper implements RowMapper<ProviderComplaintReview> {
        @Override
        public ProviderComplaintReview mapRow(ResultSet rs, int rowNum) throws SQLException {
            ProviderComplaintReview review = new ProviderComplaintReview();
            review.setComplaintId(rs.getInt("complaint_id"));
            review.setApproved(rs.getBoolean("is_approved"));
            review.setDescription(rs.getString("description"));
            review.setRatingDeduction(rs.getInt("rating_deduction"));
            return review;
        }
    }
}
